package org.ccay.manage.registry.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.ioc.Ccay;
import org.ccay.core.registry.IRegistryManager;
import org.ccay.core.registry.RegistryItem;
import org.ccay.core.request.Application;
import org.ccay.manage.registry.service.IRegistryExtInfoService;
import org.ccay.manage.registry.vo.RegistryItemFullInfoVO;
import org.ccay.manage.registry.vo.RegistryTreeNodeVO;
import org.ccay.registry.RegistryManager;
import org.ccay.security.util.SecurityUtil;


@Named
public class RegistryExtInfoService implements IRegistryExtInfoService {

	@Inject
	private IRegistryManager registryManager;
	
	
	private List<String> registryPatternWhiteList;
	
	@Override
	public void deleteRegistry(String registryItemName) throws ApplicationException {
		registryManager.deleteRegistryItem(registryItemName);
	}

	@Override
	public void createOrUpdateRegistryItem(RegistryItem item) throws ApplicationException {
		registryManager.createOrUpdateRegistryItem(item);

	}

	@Override
	public RegistryItemFullInfoVO findRegistry(String registryItemName) {
		RegistryItemFullInfoVO vo = new RegistryItemFullInfoVO();
		vo.setName(registryItemName);
		vo.setValue(registryManager.getValue(registryItemName));
		RegistryManager manager = (RegistryManager)registryManager;
		RegistryItem regFromExt = manager.getExtRegistroyContainer().get(registryItemName);
		if(null != regFromExt){
			vo.setValueFromExtInfo(regFromExt.getValue());
			vo.setDesc(regFromExt.getDesc());
			//扩展定义中都是可覆盖
			vo.setOverwritable(true);
			vo.setDefinedInExtStore(true);
		}
		RegistryItem regFromFile = manager.getFileRegistroyContainer().get(registryItemName);
		if(null != regFromFile){
			vo.setValueFromSourceCode(regFromFile.getValue());
			//都定义描述时，拼接描述，且扩展信息的在前
			vo.setDesc((vo.getDesc() == null ? "" : vo.getDesc()+"|" )+ regFromFile.getDesc());
			vo.setOverwritable(regFromFile.isOverwritable());
			vo.setDefinedInCodeSource(true);
		}
		return vo;
	}

	@Override
	public List<RegistryTreeNodeVO> listAll() {
		List<RegistryItem> items = registryManager.getAll();
		List<RegistryTreeNodeVO> root = new LinkedList<RegistryTreeNodeVO>();
		
		for(RegistryItem item : items){
			String[] path = item.getName().split("\\.");
			List<RegistryTreeNodeVO> children = root;
			for(int i=0;i<path.length;i++){
				String pathNode= path[i];
				RegistryTreeNodeVO node = findNode(pathNode,children);
				if(node == null){
					node = new RegistryTreeNodeVO(pathNode);
					if(i == path.length-1){//子
						node.setRegistryItemName(item.getName());
					}
					children.add(node);
				}
				children = node.getChildren();
			}
		}
		return root;
	}
	

	public RegistryTreeNodeVO findNode(String path,List<RegistryTreeNodeVO> children)
	{
		for(RegistryTreeNodeVO node : children){
			if(path.equals(node.getName())){
				return node;
			}
		}
		return null;
	}

	@Override
	public RegistryItem findRegistryItem(String itemName) {
		registryPatternWhiteList = null;
		if(registryPatternWhiteList == null){
			try{
				registryPatternWhiteList = (List<String>) Ccay.getContext().getBean("registryPatternWhiteList");
			}catch(Exception e){
				//没有定义
				registryPatternWhiteList = new ArrayList<String>();
			}
			//许访问引用名称及版本号
			registryPatternWhiteList.add("Ccay\\.GlobalParamters\\.AppVersion");
		}
		boolean allow = false;
		for(String pattern : registryPatternWhiteList){
			if(itemName.matches(pattern)){
				allow = true;
				break;
			}
		}
		if(allow){
			return registryManager.get(itemName);
		}
		return null;
	}

	@Override
	public void reloadAll() {
		//开发环境不校验权限
		if(Application.getCurrent().getEnviorment().equals(Application.DEV)){
			registryManager.reload();
		}else{
			SecurityUtil.checkPermission("registryManage:reload");
			registryManager.reload();
		}
	}
}
